<html>
	<head> <title>My Little Calculator </title> </head>
	
	<body>
	<h1>My Little Calculator</h1>
	<h3>By Hong Shin</h3>

	<form action="./calculator.php" method="get">
	Expression: <input type="text" name="expr" />
	<input type="submit" />
	</form>
		
	<?php
		$op = $_GET["expr"];

		if( $op != null )
		{
			# make sure that any characthers that are not
			# numeric or in: , + - * / are not allowed. 
			if (preg_match('/[^0-9\.\+\-\*\/\ ]/', $op) or 
			# inelegant, but did not know how to use the | (or) 
			# in php regex syntax. so I will use this. 

			# some operators cannot immediately after another:
			# e.g. 2 ** 3 is bad. So is 5 +++ 2 or 3...34

			#Let's start w/ the '.'
			# e.g. .. or ..., etc are bad.
			    preg_match('/\.[\.]{1,}/', $op) or
			
			# handle the * case:
			# notice that *- is allowed. For example, 3*-2 = -6.
			# but, stuff like ** or *+ or */ are bad.
			    preg_match('/\*[\.\+\*\/]{1,}/', $op) or

			# handle the / case:
			# notice that /- is allowed. For example, -2/-3 = .06667.
			    preg_match('/\/[\.\+\*\/]{1,}/', $op) or

			# handle the + case:
			# note that +- is allowed. For exmaple, 2+-2 = 0.
			    preg_match('/\+[\.\+\*\/]{1,}/', $op) or

			# handle the - case:
			# note that -+ is allowed. For example, 2-+3 = -1.
			    preg_match('/\-[\.\-\*\/]{1,}/', $op) or
			
			# hanging operators, like just '+' all by itself
			# is bad, so I handle that case here:
				preg_match('/^[\.\+\-\*\/]$/', $op))
			
			# NOTE: I do not check for this anymore, because
			# Chu-cheng's implementation does not either. 
			# OLD:: ## make sure that we cannot divide by zero:
			# OLD::	##preg_match('/.*\/0/', $op)) 
			{
				echo "<h1>" . $op . " is not a valid expression." . "</h1>";
			}
			# Otherwise, we have a good expression (hopefully!)
			# so we will go ahead and evaluate it.
			else
			{
				echo "<h1>Result</h1>";
				# We place the '@' to suppress the warning
				# when dividing by zero.
				$value=@eval("return ($op);");
				echo "<h2>" . $op . " = " . $value . "</h2>";
			}

		}
		else 
		{
		#	echo "null";
		}


	?>

	</body>

</html>
